package org.extreme.pivot.grid;

import java.awt.Color;
import java.awt.Graphics2D;

import org.apache.pivot.wtk.Component;
import org.apache.pivot.wtk.skin.ComponentSkin;

public class GridRowSkin extends ComponentSkin {
	Color gridLineColor = Color.GRAY;
	Color labelColor = Color.RED;
	GridRow gridRow;

	@Override
	public void layout() {
		// No-op
	}

	@Override
	public int getPreferredHeight(int width) {
		return 0;
	}

	@Override
	public int getPreferredWidth(int height) {
		return 72;
	}

	@Override
	public void paint(Graphics2D graphics) {
		graphics.setColor(Color.LIGHT_GRAY);
		graphics.fillRect(0, 0, getWidth(), getHeight());
		
		paintGridLine(graphics);
		
		paintLabels(graphics);
	}
	
	@Override
    public void install(Component component) {
		super.install(component);
		
		this.gridRow = (GridRow)component;
	}

	void paintGridLine(Graphics2D graphics) {
		Color oldColor = graphics.getColor();
		graphics.setColor(this.gridLineColor);

		for(int currentY = 0, r = gridRow.xSheetPane.getScrollTop(); currentY < getHeight(); r ++) {
			graphics.drawLine(0, currentY, getWidth(), currentY);
			currentY += gridRow.xSheetPane.rowHeightList.get(r);
		}
		
		for(int currentX = 0, c = gridRow.xSheetPane.getScrollLeft(); currentX < getWidth(); c ++) {
			graphics.drawLine(currentX, 0, currentX, getHeight());
			currentX += gridRow.xSheetPane.colWidthList.get(c);
		}
		graphics.setColor(oldColor);
	}
	
	void paintLabels(Graphics2D graphics) {
		Color oldColor = graphics.getColor();
		graphics.setColor(this.labelColor);
		
				
		for(int currentY = 0, r = gridRow.xSheetPane.getScrollTop(); currentY < getHeight(); r ++) {
			int cellHeight = gridRow.xSheetPane.rowHeightList.get(r);
			graphics.drawString(gridRow.getColRowRenderer().render(r).toString(), 0, currentY + cellHeight);
			currentY += cellHeight;
		}
		
		graphics.setColor(oldColor);
	}
}
